require(['../config'], function () {
	require(['jquery', 'avalon', 'util', 'router', 'text!/assess/details.html', 'text!/assess/result.html','Cookies','text!/common/nav.html','text!/common/footer.html','navInit'], function ($, avalon, util, router, detailsTmpl, resultTmpl,Cookies,
		navHtml,
		footerHtml,
		navInit) {
			$(".global-nav").html(navHtml);
			$(".global-footer").html(footerHtml);
			navInit();

		
		if (!(localStorage.token && (localStorage.exp - new Date().getTime()) > 0)){
			alert("请先登陆");
			window.location.href = "/login.html?redirect_uri=" + encodeURIComponent(window.location.pathname);
			return;
		}

		if (parseInt(localStorage.userType) !== 1){
			if (confirm("请以会员身份登陆才可以进行精准测评"))
			{
				window.location.href = "/login.html?redirect_uri=" + encodeURIComponent(window.location.pathname);
			}
			else
			{
				window.location.href = "/";
			}
		}

		var vm = avalon.define({
			$id: 'assess-details',
			data: {},
			score: 0, // 评分
			money: 0, // 可以贷款金额
			params: {
				professional: 0,
				houseType: 0,
				age: '25',
				provinceId: 320000,
				cityId: 320100,
				loanAmount: 10,
				loanTime: 1,
				takeLoanTimes: 0,
				credit: 0,
				carType: 0,
				marryType: 0,
				salary: 0,
				loanType: 0,
				payment: 1,
				provinceName:'江苏省',
				cityName:'南京市'
			},
			imgUrl: function (item) {
				return item.logo || 'http://img0.rongsue.com/images/channel/rec-logo.png';
			},
			productName: function (item) {
				return item.instName + '-' + item.name;
			},
			tags: function (item) {
				var list = [];
				var t = item.featuresText.split(',');
				$.each(item.features.split(','), function (i, str) {
					list.push('<span class="f' + str + '">' + t[i] + '</span>');
				})
				return list.join('');
			},
			info: function (item) {
				return '贷款最高' + item.loanMax + '万元，' + item.takeTimesText + '内放款，利率' + item.rateMin + '%';
			},
			desc: function (item) {
				return item.requirement;
			},
			result: function () {
				var data = vm.$model;
				util.ajax({
					url: '/assess/details',
					type: 'post',
					data: data.params
				}).then(function (rsp) {
					vm.score = rsp.data.score;
					vm.money = rsp.data.money;
					// fengyanan说需要支持返回到精准测评表单页，所以让其产生历史记录
					avalon.router.navigate('/result', 2)//默认打开，不改变url
				});
				// 获取推荐列表
				this.recommend();
			},
			recommend: function () {
				var data = vm.$model;
				util.ajax({
					url: '/assess/details/recommend',
					type: 'post',
					data: data.params,
					success: function (rsp) {
						vm.data = rsp.data;
					}
				});
			}
		});

		avalon.router.add("/", function () {
			$('#router-view').html(detailsTmpl);
			avalon.scan(document.getElementById('router-view'))
		})
		avalon.router.add("/result", function () {
			$('#router-view').html(resultTmpl);
			avalon.scan(document.getElementById('router-view'))
		})

		avalon.history.start({
			root: '/'
		})

		var hash = location.hash.replace(/#!?/, '')

		// 模式0, 不改变URL, 不产生历史实体, 执行回调
		// 模式1, 改变URL, 不产生历史实体,   执行回调
		// 模式2, 改变URL, 产生历史实体,    执行回调
		avalon.router.navigate(hash || '/', 2)

		avalon.scan(document.getElementById('router-view'))

	})
});